<?php
	function &load_class($name=false)
	{
		static $classes = array();
		if($name === false)
		{
			return $classes;
		}
		else
		{
			debug("load_class: called (Name: ".$name.")");
			if(!isset($classes[$name]))
			{
				debug("load_class: including class file (Name: ".$name.")");
				require(LIBDIR.$name.".php");
				debug("load_class: instantiating class (Name: ".$name.")");
				$classes[$name] =& new $name();
				debug("load_class: returning new instance (Name: ".$name.")");
				return $classes[$name];
			}
			else {
				debug("load_class: returning existing instance (Name: ".$name.")");
				return $classes[$name];
			}
		}
	}
	function &get_instance()
	{
		$temp =& load_class("library");
		return $temp;
	}
	
	function debug($message=false)
	{
		static $messages;
		if($message === true)
		{
			print("<strong>Debug Messages</strong><br />");
			foreach ( $messages as $num => $msg )
			{
				print("[".$num."] ".$msg."<br />");
			}
		}
		elseif ($message !== false)
		{
			$messages[] = $message;
		}
	}
	function error ($msg,$die=false)
	{
		// Will eventually make this a pretty error page!
		global $start;
		if($die !== false){
			$exec_time = (microtime(true)-$start);
			debug("Execution time: ".$exec_time." seconds.");
			debug(true);
			die("Fatal: ".$msg);
		}
		else{
			print($msg);
		}
	}	
	
	function error_handler($num,$msg,$file,$line,$context)
	{	
		die("Error (".$num."): ".$msg." (Line: ".$line.", File: ".$file.", Context: ".print_r($context,true).")");
	}
?>